package colthgame.app;

import android.graphics.Rect;
import android.graphics.Point;
import android.view.MotionEvent;

public class ItemCostume extends RenderGameObject
{

	enum E_Layer
	{
		ePANTS, eTOP, eSCARF, eBANGLE, eSOCKS, eSHOSE, eHAIRPIN, eRING, eBACK, eMAX
	}

	static final public E_Layer[]	ELayer	= E_Layer.values();

	ItemCostume(E_Layer _eLayer)
	{
		super(BaseGameObject.E_Type.eCOSTUME);
		m_eLayer = _eLayer;
		m_bSelected = false;
		m_bEquipped = false;
		m_Rect = new Rect();
		m_lLastClickMS = 0;
	}

	public E_Layer GetLayer()
	{
		return m_eLayer;
	}

	public boolean onTouchEvent(MotionEvent event, Character _character)
	{

		CalculateRect();
		Point TouchPoint = new Point((int) event.getX(), (int) event.getY());

		boolean bResult = false;
		switch (event.getAction())
		{
			case MotionEvent.ACTION_DOWN:
			{
				if (CollisionMgr.GetInstance().IntersectRECT_POINT(m_Rect, TouchPoint))
				{
					m_bSelected = true;
					_character.SetRender(m_eLayer, true);
					bResult = true;
					if( (System.currentTimeMillis() - m_lLastClickMS <= 200) )
					{
						Point p = _character.GetCostumePos(m_eLayer);
						SetPosX(p.x);
						SetPosY(p.y);
						m_bEquipped = true;
					}
					else
					{
						m_bEquipped = false;
					}
					m_lLastClickMS = System.currentTimeMillis();
				}
			}
			break;
			case MotionEvent.ACTION_MOVE:
			{
				if (true == m_bSelected)
				{
					SetPosX(TouchPoint.x);
					SetPosY(TouchPoint.y);
					bResult = true;
				}
			}
			break;
			case MotionEvent.ACTION_UP:
			{
				if (true == m_bSelected)
				{
					SetPosX(TouchPoint.x);
					SetPosY(TouchPoint.y);
					_character.SetRender(m_eLayer, false);
					if (true == _character.Intersect(m_eLayer, m_Rect) )
					{
						Point p = _character.GetCostumePos(m_eLayer);
						SetPosX(p.x);
						SetPosY(p.y);
						m_bEquipped = true;
					}
					else
					{
						m_bEquipped = false;
					}
					bResult = true;
				}
				m_bSelected = false;
			}
			break;
		}
		return bResult;
	}

	public void CalculateRect()
	{
		m_Rect.left = GetPosX() - GetWidth_Half();
		m_Rect.top = GetPosY() - GetHeight_Half();
		m_Rect.right = GetPosX() + GetWidth_Half();
		m_Rect.bottom = GetPosY() + GetHeight_Half();
	}

	public boolean IsEquipped()
	{
		return m_bEquipped;
	}

	public void SetInitPosX(int nPosX)
	{
		m_nInitPosX = nPosX;
		SetPosX(m_nInitPosX);
	}

	public void SetInitPosY(int nPosY)
	{
		m_nInitPosY = nPosY;
		SetPosY(m_nInitPosY);
	}

	public void ChangeInitPos()
	{
		SetPosX(m_nInitPosX);
		SetPosY(m_nInitPosY);
	}

	private E_Layer	m_eLayer;
	private boolean	m_bSelected;	// 마우스로 클릭된 상태인지
	private boolean	m_bEquipped;	// 캐릭터에 장착된 상태인지
	private Rect	m_Rect;
	private long	m_lLastClickMS;
	private int		m_nInitPosX;	// 초기 위치값
	private int		m_nInitPosY;	// 초기 위치값
}
